package view
{
	import com.bit101.components.PushButton;
	import com.bit101.components.Text;
	import com.bit101.components.VBox;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import models.ModelLocator;
	
	public class ProportionList extends VBox
	{
		private var model:ModelLocator = ModelLocator.getInstance();
		private var ary:Array = model.sizeAry;
		public function ProportionList(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			initnew();
		}
		private function inits():void{
			var btn:TextField = getText("");
			btn.visible = false;
			this.addChild(btn);
			for each (var prop:Object in ary)
			{
				btn = getText(prop.w + "×" +  prop.h);
				this.addChild(btn);
				btn.addEventListener(MouseEvent.CLICK,onClick);
			}
			btn = getText("");
			btn.visible = false;
			this.addChild(btn);
		}
		private function initnew():void{
			var txt:TextField = new TextField();
			txt.width = 12;
			txt.height = 12;
			txt.selectable = false;
			this.addChild(txt);
			for each (var prop:Object in ary){
				var btn:ListButton = new ListButton;
				btn.setData(prop);
				this.addChild(btn);
			}
			txt = new TextField();
			txt.width = 12;
			txt.height = 12;
			txt.selectable = false;
			this.addChild(txt);
			this.dispatchEvent(new Event("resizeevent"));
		}
		public function resize():void{
			this.onInvalidate();
			this.dispatchEvent(new Event("resizeevent"))
		}
		private function onClick(event:MouseEvent):void{
			var str:String = event.target.text;
			var ary:Array = str.split("×");
			SizeShow.getInstance().onClick(ary[0],ary[1]);
			model.size = ary[0] + "*" + ary[1];
			loadType(model.size)
		}
		private function getText(str:String):TextField{
			var txt:TextField = new TextField();
			txt.width = 100;
			txt.height = 20;
			txt.htmlText = "<p align='center'><a href='event:myevent'><font>" + str + "</font></a></p>";
			txt.selectable = false;
			txt.border = true;
			txt.borderColor = 0xbbb5ac;
			return txt;
		}
		private function loadType(str:String):void{
			model.boderAry = null;
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE,onComplete)
			str = model.config.type //+ "?size=" + str;
			urlloader.load(new URLRequest(str));
		}
		private function onComplete(event:Event):void{
			var xml:XML = XML(event.target.data);
			model.setBoderAry(xml);
		}
		
	}
}